var express = require('express');
var router = express.Router();
// let User = require("./../models/user");
var config = require('../common/config');
let status = require('../common/statusConfig');
// var mysql = require('mysql');
const axios = require('axios')
// let mongoose = require('mongoose');

//链接mongo数据库
// mongoose.connect("mongodb://127.0.0.1:27017/testMongoDB");
// mongoose.connection.on("connected", function () {
//     console.log("mongodb connect success")
// });
// mongoose.connection.on("error", function () {
//     console.log("1")
// });

// api.getAccessToken(function (err, token) {
//     var access_token = token.accessToken;
//     console.info(token)
// });
// router.all('*', function (req, res, next) {
//     res.header('Access-Control-Allow-Origin', '*');
//     res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
//     res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
//
//     if (req.method == 'OPTIONS') {
//         res.send(200);
//         /让options请求快速返回/
//     }
//     else {
//         next();
//     }
// });
/* 获取服务以及商品列表 */
router.get('/', function (req, res, next) {
    const url =`${config.ctx}/servicelistapp.htm`
    let data = {
        pageSize: req.query.pageSize,
        area_id: "",
        service_type: 1,
        currentPage: req.query.currentPage
    }
    axios.get(url, {
        params: data
    }).then((response) => {
        response = response.data
        res.json({
            code: status.success,
            msg: '',
            result: {
                productList: response.list,
                totalPage: response.totalPage
            }
        })
    }).catch((e) => {
        console.log(e)
        res.json({
            code: status.fail,
            msg: '获取商品列表失败'
        })
    })
    // let db = mysql.createConnection(config.mysqlInfo);
    // db.connect();
    // db.query(getHomeStr, [10], (err, data) => {
    //     if (err) {
    //         console.log(err);
    //         res.status(500).send('database err').end();
    //     } else {
    //         if (data.length == 0) {
    //             res.status(500).send('no datas').end();
    //         } else {
    //             let newData=[];
    //             data.forEach(function (item, index) {
    //                 getAreaName(item.area_id).then(function (res2) {
    //                     data[index].areaName = res2[0]
    //                     newData=data[index]
    //                 })
    //                 if (index === data.length - 1) {
    //                     res.send(newData);
    //                 }
    //             })
    //
    //         }
    //     }
    // });
    // db.end();
});

module.exports = router;
